package com.yyy.modules;

import com.yyy.common.exception.QueueNoFoundException;
import com.yyy.modules.core.MsgDispatcherService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author yyy
 * @Date 2020-09-13 14:16
 * @Email yangyouyuhd@163.com
 */
//@Component
public class MsgTemplate<T> {


    List<String> names;

    private MsgDispatcherService dispatcherService;

    @Autowired
    public MsgTemplate(MsgDispatcherService dispatcherService) {
        this.dispatcherService = dispatcherService;
        this.names = dispatcherService.getNameList();
    }

    /**
     * 解析路由、获取匹配的队列名
     * 封装消息体  ot msagger
     * 根据匹配的队列名获取 队列、
     * 将消息 推入队列
     *
     * @param route
     * @param body
     */
    public void send(String route, Object body) {
        if (!names.contains(route)) {
            throw new QueueNoFoundException("不存在指定的队列");
        }
        dispatcherService.send(route, body);
    }
}
